package com.aistar.service.impl;

import com.aistar.mapper.ResumeMapper;
import com.aistar.pojo.*;
import com.aistar.service.*;
import com.aistar.util.MyBatisUtil;
import com.aistar.util.ServerResponse;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ResumeServiceImpl implements ResumeService {

    // select  count(resumr_id) from resume where custid=2 and status = 0 and resume_name =?

    @Override
    public ServerResponse save(Resume resume) {
        SqlSession session = MyBatisUtil.getSqlSession();
        ResumeMapper mapper = session.getMapper(ResumeMapper.class);

        ServerResponse response = getCountByCustId(resume.getCustId(),resume.getResumeName());
        if(response.getStatus() == 1){ // 简历不存在
            int rows =  mapper.insert(resume);
            session.commit();
            MyBatisUtil.closeSqlSession();
            if(rows >0)
                return ServerResponse.addSuccess();
            return ServerResponse.addFailed();
        }else{
            return response;  //return ServerResponse.getDataFailed("该简历名称已存在，请重命名简历名称");
        }
    }


    //查询当前用户最新添加的简历
    public ServerResponse getLatestOne(Integer custId){
        SqlSession session = MyBatisUtil.getSqlSession();
        ResumeMapper mapper = session.getMapper(ResumeMapper.class);

        ResumeExample example = new ResumeExample();
        example.createCriteria().andCustIdEqualTo(custId).andStatusEqualTo(0);
        example.setOrderByClause("resume_create_time desc" );

       Resume resume =  mapper.selectByExample(example).get(0);
        MyBatisUtil.closeSqlSession();
        return ServerResponse.getDataSuccess(resume);
    }

    @Override
    public ServerResponse getAllByCustId(Integer custId) {
        SqlSession session = MyBatisUtil.getSqlSession();
        ResumeMapper mapper = session.getMapper(ResumeMapper.class);
        ResumeExample example = new ResumeExample();
        example.createCriteria().andCustIdEqualTo(custId).andStatusEqualTo(0);

        List<Resume> resumeList =  mapper.selectByExample(example);
        System.out.println(resumeList.size());
        MyBatisUtil.closeSqlSession();
        return ServerResponse.getDataSuccess(resumeList);

    }

    @Override
    public ServerResponse getResumeAllVOById(Integer resumeId) {
        ResumeAllVO resumeAllVO = new ResumeAllVO();

        SqlSession session = MyBatisUtil.getSqlSession();
        ResumeMapper mapper = session.getMapper(ResumeMapper.class);


        Resume resume =  mapper.selectByPrimaryKey(resumeId);
        resumeAllVO.setResume(resume);



        //根据简历id 获得 所有 workExperience   //	select * from work_experience where resumeId = 85 and status  =0
        WorkExperienceService workExperienceService =  new WorkExperienceServiceImpl();
       ServerResponse workExperienceServerResponse =  workExperienceService.getAllByResumeId(resumeId);
       if(workExperienceServerResponse.getStatus() ==1){
           List<WorkExperience> workExperienceList = ( List<WorkExperience>)  workExperienceServerResponse.getData();
           resumeAllVO.setWorkExperienceList(workExperienceList);
       }


        //根据简历id 获得 所有 projectExperience
        ProjectExperienceService projectExperienceService = new ProjectExperienceImpl();
       ServerResponse  projectExperienceServerResponse = projectExperienceService.getAllByResumeId(resumeId);
        if(projectExperienceServerResponse.getStatus() ==1) {
            List<ProjectExperience> projectExperienceList = (List<ProjectExperience>) projectExperienceServerResponse.getData();
            resumeAllVO.setProjectExperienceList(projectExperienceList);
        }

        //根据简历id 获得 所有 honor
        HonorService honorService = new HonorServiceImpl();
        ServerResponse hornorServerResponses = honorService.getAllByResumeId(resumeId);
        if(hornorServerResponses.getStatus() ==1){
            List<Honor> honorList =  (List<Honor>) hornorServerResponses.getData();
            resumeAllVO.setHonorList(honorList);
        }
        //根据简历id 获得 所有 skill
        SkillService skillService = new SkillServiceImpl();
        ServerResponse skillServerResponses = skillService.getAllByResumeId(resumeId);
        if(skillServerResponses.getStatus() ==1){
            List<Skill> skillList =  (List<Skill>) skillServerResponses.getData();
            resumeAllVO.setSkillList(skillList);
        }
        MyBatisUtil.closeSqlSession();
        return ServerResponse.getDataSuccess(resumeAllVO);
    }

    @Override
    public ServerResponse modifyStatus(Resume resume) {
        return null;
    }

    @Override
    public ServerResponse modify(Resume resume) {
        SqlSession session = MyBatisUtil.getSqlSession();
        ResumeMapper mapper = session.getMapper(ResumeMapper.class);
         int rows = mapper.updateByPrimaryKeySelective(resume);  //update set resume_name =? , resume_update_time = ? where resume_id= ?
         session.commit();
         MyBatisUtil.closeSqlSession();

         if(rows >0)
             return ServerResponse.updateSuccess();
        return ServerResponse.updateFailed();
    }

    @Override
    public ServerResponse getById(Integer id) {
        SqlSession session = MyBatisUtil.getSqlSession();
        ResumeMapper mapper = session.getMapper(ResumeMapper.class);
        Resume resume = mapper.selectByPrimaryKey(id);
        MyBatisUtil.closeSqlSession();
        return ServerResponse.getDataSuccess(resume);
    }

    @Override
    public ServerResponse getAll() {
        return null;
    }

    @Override
    public ServerResponse getCountByCustId(Integer custId) {

        //   select count(resume_id) from resume where cust_id = ? and status = 0
        SqlSession session = MyBatisUtil.getSqlSession();
        ResumeMapper mapper = session.getMapper(ResumeMapper.class);

        ResumeExample example = new ResumeExample();
        example.createCriteria().andCustIdEqualTo(custId).andStatusEqualTo(0);

        int count = mapper.countByExample(example);
        MyBatisUtil.closeSqlSession();
        System.out.println("该用户的已有简历的个数：" + count);
        if(count <3){
            return ServerResponse.getDataSuccess("可以创建新简历");
        }
        return ServerResponse.getDataFailed("最多只能创建3份简历");
    }

    @Override
    public ServerResponse getCountByCustId(Integer custId, String resumeName) {
        SqlSession session = MyBatisUtil.getSqlSession();
        ResumeMapper mapper = session.getMapper(ResumeMapper.class);

        ResumeExample example = new ResumeExample();
        example.createCriteria().andCustIdEqualTo(custId).andStatusEqualTo(0).andResumeNameEqualTo(resumeName);

        int count = mapper.countByExample(example);
        MyBatisUtil.closeSqlSession();
        System.out.println("该用户的已有简历的个数：" + count);
        if(count ==0){
            return ServerResponse.getDataSuccess("简历不存在");
        }
        return ServerResponse.getDataFailed("该简历名称已存在，请重命名简历名称");
    }
}
